<?php


namespace app\admin\controller;


use think\admin\Controller;
use think\Db;

/**
 * 远程巡店
 */
class PatrolShop extends Controller
{

//    /**
//     * 绑定数据表
//     * @var string
//     */
//    private $table = 'lgPatrolShopWarning';

    /**
     * 巡店列表
     * @auth true
     * @login true
     */
    public function index()
    {
        $this->title = '巡店功能';
        $this->shopList = $this->app->db->name('lg_distributor')
            ->where('status', 1)->field('id,name')->select()->toArray();
        //搜索
        $shop_id = input('shop_id');
        $shop_name = input('shop_name');
        $equip_id = input('equip_id');
        $channel_id = input('channel_id');
        $shop_clerks_name = input('shop_clerks_name');
        $status = input('status');
        $inspection_time = input('inspection_time');
        $where = [];
        if ($shop_id) {
            $shop_id=trim(trim($shop_id,' '));
            $where[] = ['shop_id', '=', $shop_id];
        }
        if ($shop_name) {
            $shop_name=trim(trim($shop_name,' '));
            $where[] = ['shop_name', 'like', '%' . $shop_name . '%'];
        }
        if ($equip_id) {
            $equip_id=trim(trim($equip_id,' '));
            $where[] = ['equip_id', 'like', '%' . $equip_id . '%'];
        }
        if ($channel_id) {
            $channel_id=trim(trim($channel_id,' '));
            $where[] = ['channel_id', 'like', '%' . $channel_id . '%'];
        }
        if ($shop_clerks_name) {
            $shop_clerks_name=trim(trim($shop_clerks_name,' '));
            $where[] = ['shop_clerks_name', 'like', '%' . $shop_clerks_name . '%'];
        }
        if ($status) {
            $status=trim(trim($status,' '));
            $where[] = ['status', '=', $status];
        }
        if ($inspection_time) {
            $inspection_time=trim(trim($inspection_time,' '));
            $inspection_time = explode(' - ', $inspection_time);
            $nt=date('Y-m-d',strtotime($inspection_time[0]));
            $et=date('Y-m-d 23:59:59',strtotime($inspection_time[1]));
            $inspection_time[0]=$nt;
            $inspection_time[1]=$et;
            $where[] = ['inspection_time', 'between time', $inspection_time];
        }

        //链接数据库
        $fields="id,shop_id,shop_name,shop_encrypt,shop_clerks_id,equip_id,channel_id,shop_clerks_name,inspection_picture,status,inspection_time";
        $query = $this->_query("lg_patrol_shop");
        $query->field($fields);
        $query->where($where);
        $query->order('id desc')->page();
    }

    /**
     * 预警列表
     * @auth true
     * @login true
     */
    public function warning()
    {
        $this->title = '预警功能';
        $this->shopList = $this->app->db->name('lg_distributor')
            ->where('status', 1)->field('id,name')->select()->toArray();
        //搜索
        $shop_name = input('shop_name');
        $shop_id = input('shop_id');
        $equip_id = input('equip_id');
        $channel_id = input('channel_id');
        $shop_clerks_name = input('shop_clerks_name');
        $status = input('status');
        $inspection_time = input('inspection_time');
        $where = [];
        if ($shop_id) {
            $shop_id=trim(trim($shop_id,' '));
            $where[] = ['shop_id', '=', $shop_id];
        }
        if ($shop_name) {
            $shop_name=trim(trim($shop_name,' '));
            $where[] = ['shop_name', 'like', '%' . $shop_name . '%'];
        }
        if ($equip_id) {
            $equip_id=trim(trim($equip_id,' '));
            $where[] = ['equip_id', 'like', '%' . $equip_id . '%'];
        }
        if ($channel_id) {
            $channel_id=trim(trim($channel_id,' '));
            $where[] = ['channel_id', 'like', '%' . $channel_id . '%'];
        }
        if ($shop_clerks_name) {
            $shop_clerks_name=trim(trim($shop_clerks_name,' '));
            $where[] = ['shop_clerks_name', 'like', '%' . $shop_clerks_name . '%'];
        }
        if ($status) {
            $status=trim(trim($status,' '));
            $where[] = ['status', '=', $status];
        }
        if ($inspection_time) {
            $inspection_time=trim(trim($inspection_time,' '));
            $inspection_time = explode(' - ', $inspection_time);
            $nt=date('Y-m-d',strtotime($inspection_time[0]));
            $et=date('Y-m-d 23:59:59',strtotime($inspection_time[1]));
            $inspection_time[0]=$nt;
            $inspection_time[1]=$et;
            $where[] = ['inspection_time', 'between time', $inspection_time];
        }
        //链接数据库
        $query = $this->_query("lg_patrol_shop_warning");
        $query->field('*');
        $query->where($where);
        $query->order('id desc')->page();
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_page_filter(array &$data)
    {
        foreach ($data as $key => $value) {
            $shop_id=$value['shop_id'];
            $shop_name=$value['shop_name'];
            $shop_encrypt=$value['shop_encrypt'];
            //处理门店信息
            if(empty($shop_name)||$shop_encrypt){
                $shop=$this->app->db->name("lg_distributor")->field("encrypt,name")->where(['id' => $shop_id])->find();
                if($shop){
                    $this->app->db->name("lg_patrol_shop_warning")
                        ->where(['id' => $value['id']])->update(['shop_name'=>$shop['name'],'shop_encrypt'=>$shop['encrypt']]);
                    $value['shop_name']    = $shop['name'];
                    $value['shop_encrypt'] = $shop['encrypt'];
                }
            }
            //处理店员信息
            if(empty($data['shop_clerks_id'])){
                $clerks = $this->app->db->name("lg_shop_clerks")->field("name")->where(['id' => $value['shop_clerks_id']])->find();
                if($clerks){
                    $this->app->db->name("lg_patrol_shop_warning")
                        ->where(['id' => $value['id']])
                        ->update(['shop_clerks_name'=>$shop['name']]);
                    $value['shop_clerks_name']    = $clerks['name'];
                }
            }
            $value['status_title'] = $this->status_warning[$value['status']] ?? '-';
            $data[$key] = $value;
        }
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _warning_page_filter(array &$data)
    {
        foreach ($data as $key => $value) {
            $shop_id=$value['shop_id'];
            $shop_name=$value['shop_name'];
            $shop_encrypt=$value['shop_encrypt'];
            if(empty($shop_name)||$shop_encrypt){
                $shop=$this->app->db->name("lg_distributor")->field("encrypt,name")->where(['id' => $shop_id])->find();
                if($shop){
                    $this->app->db->name("lg_patrol_shop_warning")
                        ->where(['id' => $value['id']])->update(['shop_name'=>$shop['name'],'shop_encrypt'=>$shop['encrypt']]);
                    $value['shop_name']    = $shop['name'];
                    $value['shop_encrypt'] = $shop['encrypt'];
                }
            }
            $value['status_title'] = $this->status_warning[$value['status']] ?? '-';
            $data[$key] = $value;
        }
    }

    public function look_index_pc(){
        $patrol_shop_id = input('id');
        $inspection_picture="";
        if($patrol_shop_id){
            $query = $this->_query("lg_patrol_shop");
            $query->where("id=".$patrol_shop_id);
            $query->value("inspection_picture");
            $inspection_picture = $this->app->db->name("lg_patrol_shop")->where(['id' => $patrol_shop_id])->value("inspection_picture");
        }
        $this->assign("inspection_picture",$inspection_picture);
        $this->fetch();

    }

    public function look_warning_pc(){
        $patrol_shop_id = input('id');
        $inspection_picture="";
        if($patrol_shop_id){
            $query = $this->_query("lg_patrol_shop");
            $query->where("id=".$patrol_shop_id);
            $query->value("inspection_picture");
            $inspection_picture = $this->app->db->name("lg_patrol_shop_warning")
                ->where(['id' => $patrol_shop_id])->value("inspection_picture");
        }
        $this->assign("inspection_picture",$inspection_picture);
        $this->fetch();

    }

    private $status = [
        1 => '正常',
        2 => '不正常',
    ];

    private $status_warning = [
        1 => '正常',
        2 => '报警',
    ];

}